package com.java113.spring.demo.controller;

import com.java113.spring.demo.model.UserInfo;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

//@RestController
@Controller
@RequestMapping("/response")
public class RespController {
    //只返回数据
    @ResponseBody
    @RequestMapping("/returnData")
    public String returnData() {
        return "hello.html";
    }

    //返回页面hello.html
    @RequestMapping("/returnPage")
    public String returnPage() {
        return "/hello.html";
    }

    //返回页面hello2.html
    @RequestMapping("/returnPage2")
    public String returnPage2() {
        return "/test/hello2.html";
    }

    @ResponseBody
    @RequestMapping("/returnHTML2")
    public String returnHTML2() {
        return "<h2>我是二级标题</h2>";
    }

    @ResponseBody
    @RequestMapping(value = "/returnPlain",produces = "text/plain")
    public String returnPlain() {
        return "<h2>我是二级标题</h2>";
    }

    //设置状态码 不影响页面显示
    @ResponseBody
    @RequestMapping("/setStatus")
    public UserInfo setStatus(HttpServletResponse response) {
        response.setStatus(404);
        UserInfo userInfo = new UserInfo("zhangsan",13);
        return userInfo;
    }

    //设置header
    //设置content-type
    @ResponseBody
    @RequestMapping(value = "/setContentType",produces = "application/Json")
    public String setContentType() {
        return "content-type设置成功";
    }

    //设置其他header
    @ResponseBody
    @RequestMapping("/setHeader")
    public String setHeader(HttpServletResponse response) {
        response.setHeader("bit","112");
        return "设置header成功";
    }

    //返回JSON数据
    @ResponseBody
    @RequestMapping("/returnJson")
    public Map<String,String> returnJson() {
        HashMap<String,String> map = new HashMap<>();
        map.put("java","113");
        map.put("python","114");
        map.put("C++","113");
        return map;
    }
}
